package com.example.zhang;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextClock;
import android.widget.TextView;

public class FirstFragment extends Fragment {
    private EditText input;
    private TextView output;
    private static final String TAG = "a";
    private static final String APP_ID = "20210607000856459";
    private static final String SECURITY_KEY = "j3yx0pqj2Af3z6KUFVJq";
    private String out;
    private String in;
    private Handler handler;
    private TextClock clock;
    private int i = 2;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       return inflater.inflate(R.layout.activity_first_fragment, container, false);

    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onActivityCreated(Bundle savedInstancesState) {
        super.onActivityCreated(savedInstancesState);
        Button button_en = getActivity().findViewById(R.id.button_en);
        Button button_zh =getActivity().findViewById(R.id.button_zh);
        Button button_change = getActivity().findViewById(R.id.button_change);
        input =getActivity().findViewById(R.id.input2);
        output = getActivity().findViewById(R.id.textView5);


        clock = getActivity().findViewById(R.id.clock);
        clock.setFormat24Hour("yyyy-MM-dd HH:mm, EEEE");


        button_en.setOnClickListener(new View.OnClickListener() { //实现从中文到英文
            @Override
            public void onClick(View v) {
                in = input.getText().toString();
                Getout_en m = new Getout_en(APP_ID, SECURITY_KEY,in);
                String out = null;
                try {
                    out = m.get_result();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                output.setText(out);

            }
        });

        button_zh.setOnClickListener(new View.OnClickListener() {  //实现从英文到中文
            @Override
            public void onClick(View v) {
                in = input.getText().toString();
                Getout_zh m = new Getout_zh(APP_ID, SECURITY_KEY,in);
                String out = null;
                try {
                    out = m.get_result();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                output.setText(out);

            }
        });

        button_change.setOnClickListener(new View.OnClickListener() {  //切换翻译功能
            @Override
            public void onClick(View v) {
                if(i%2==0) {
                    button_en.setVisibility(View.GONE);
                    button_zh.setVisibility(View.VISIBLE);
                    button_change.setText("英————>中（点击切换）");
                }else{
                    button_en.setVisibility(View.VISIBLE);
                    button_zh.setVisibility(View.GONE);
                    button_change.setText("中————>英（点击切换）");
                }
                i=i+1;
            }
        });
    }


}
